package com.day1013.morning;
//  抢票线程类
public class TicketThread implements Runnable {
    private int count = 10; //   票的数量
    private int num = 0;  //   抢到了几张票
    private boolean flag = true;

//    public synchronized void sale(){
//        if (count <= 0){
//            flag = false;
//            return;
//        }
//        count -- ;
//        num ++ ;
//        System.out.println(Thread.currentThread().getName() + "抢到了" + num + "张票,还剩" + count + "张票。");
//        try {
//            Thread.sleep(500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    @Override
    public void run() {
        while (flag == true){
            synchronized (this){
                if (count <= 0){
                    flag = false;
                    return;
                }
                count -- ;
                num ++ ;
                System.out.println(Thread.currentThread().getName() + "抢到了" + num + "张票,还剩" + count + "张票。");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                //sale();
        }
    }
}
